/*
 * Nightc-FileUpload
 * Copyright (c) 2015 nightc http://blog.nightc.com/
 * Date: 2015-07-23
 * 
 */
(function($){
	$.fn.FileUpload = function(options){
		var defaults = {
            maxFileNum: 0,//最大上传数目
            callback:function(arr){},
            posturl:"",
            filename: 'file',
            resp_url: 'true_url',
            resp_info: 'info',
            formId:'',
            InputName:'',
        }
        var fileUploaded = 0 //设置当前的上传数目
        var respArray = []
        var _this = $(this)
        var options = $.extend(defaults, options);
        var _dom_bg = "<div class=\"nightc-fileupload-bg\"></div>"
        var _dom_win = "<div class=\"nightc-fileupload-win\"><div class=\"fileupload-win-title\"><span class=\"fileupload-win-info\"></span><span class=\"fileupload-win-close\">x</span></div><div class=\"fileupload-win-body\"></div></div>"
        var _dom_input = "<input id=\"nightc-fileupload-input\" type=\"file\" \\>"
        var _dom_upload_add = "<div class=\"fileupload-btn fileupload-btn-add\">+</div>"
        var _dom_upload_ok = "<div class=\"fileupload-btn fileupload-btn-ok\">√</div>"
        var _dom_upload_delete = "<div class=\"fileupload-btn-delete\"></div>"
        var tool = {}
        tool.init = function() {
             _this.click(function() {
                tool.show()
            })
        }
        tool.show = function() {
            //展示插件
            if ($('.nightc-fileupload-bg').length == 0) {
                $("body").append(_dom_bg)
                $('.nightc-fileupload-bg').append(_dom_win)
                $('.fileupload-win-body').append(_dom_upload_add)
                $('.fileupload-win-body').append(_dom_input)
                $('.nightc-fileupload-bg').show()
            } else {
                $('.nightc-fileupload-bg').show()
            }
            //关闭事件绑定
            $('.fileupload-win-close').click(function() {
                tool.close()
                options.callback(respArray)
            })
            //绑定上传时间
            tool.upInit()
        }
        tool.upInit = function() {
            tool.do_up()
            $('.fileupload-btn-add').click(function() {
                $('#nightc-fileupload-input').trigger('click')
            })
        }
        tool.addsuccess = function(url) {
            fileUploaded = fileUploaded + 1
            if (typeof url == "string") {
                var tmp = $(_dom_upload_ok)
                tmp.html("<img src=\""+ url +"\">")
                $('.fileupload-win-body').prepend(tmp)
            } else {
                $('.fileupload-win-body').prepend(_dom_upload_ok)
            }
        }
        tool.info = function(info) {
            if (typeof info == "string") {
                $('.fileupload-win-title').css('background-color', '#74A9DB')
                $('.fileupload-win-info').html(info)
            }
        }
        tool.err = function(err) {
            if (typeof err == "string") {
                $('.fileupload-win-title').css('background-color', '#E02629')
                $('.fileupload-win-info').html(err)
            }
        }
        tool.do_up = function() {
            $('#nightc-fileupload-input').change(function() {
                var _this_input = $(this)
                var formData = new FormData();
                formData.append(options.filename, _this_input[0].files[0]);
                $.ajax({
                    url: options.posturl,
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType : false,
                    async: false,
                    cache: false,
                    dataType: 'json',
                    success: function(d) {
                        respArray.push(d)
                        tool.addsuccess(d[options.resp_url])
                        tool.info(d[options.resp_info])
                    },
                    error: function(e) {

                    }
                })
                _this_input.val("")
            })
        }
        tool.close = function() {
            $('.nightc-fileupload-bg').hide()
        }
        tool.init()
	}
})(jQuery);